#include "Enemy.h"

Enemy::Enemy(const char *EnemyType) {
	m_dic = GameData::getInstance()->getmonsterProPerty(EnemyType);

	m_attack = atoi(m_dic->valueForKey(MONSTER_ATTACK_KEY)->getCString());
	m_HP = atoi(m_dic->valueForKey(MONSTER_HP_KEY)->getCString());
	m_moveSpeed = atoi(m_dic->valueForKey(MONSTER_SPEED_KEY)->getCString());
	m_knockbackResistance = atof(m_dic->valueForKey(MONSTER_KNOCKBACK_KEY)->getCString());
	m_givenGold = atoi(m_dic->valueForKey(MONSTER_GIVENGOLD_KEY)->getCString());
	m_givenJewel = atoi(m_dic->valueForKey(MONSTER_GIVENJEWEL_KEY)->getCString());

	m_attackAnimate = initAnimate(ATTACK_ANIMATE_KEY);
	m_deathAnimate = initAnimate(DEATH_ANIMATE_KEY);
	m_runAnimate = initAnimate(RUN_ANIMATE_KEY, -1);
	m_freezeAnimate = initAnimate(FREEZE_ANIMATE_KEY);

	CC_SAFE_RETAIN(m_attackAnimate);
	CC_SAFE_RETAIN(m_deathAnimate);
	CC_SAFE_RETAIN(m_runAnimate);
	CC_SAFE_RETAIN(m_freezeAnimate);
}

Enemy::~Enemy() {
	CC_SAFE_RELEASE(m_attackAnimate);
	CC_SAFE_RELEASE(m_deathAnimate);
	CC_SAFE_RELEASE(m_runAnimate);
	CC_SAFE_RELEASE(m_freezeAnimate);
}

Animate* Enemy::initAnimate(const char *type, int loop, float delay) {
	AnimateManager *manager = AnimateManager::getInstance();
	auto animate_dic = dynamic_cast<__Dictionary *>(m_dic->objectForKey(type));
	const char *frameName = animate_dic->valueForKey(ANIMATE_FRAME_NAME_KEY)->getCString();
	int frameCount = atoi(animate_dic->valueForKey(ANIMATE_FRAME_COUNT_KEY)->getCString());
	return manager->getAnimate(frameName, frameCount, loop, delay);
}